package com.gjy.design.observer.disruptor;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-11-08 09:17:40
 */
public class PubSubSystemDemo {

    public static void main(String[] args) throws InterruptedException {
        // 创建系统（缓冲区大小1024，4个事件处理器）
        HighPerformancePubSubSystem system = new HighPerformancePubSubSystem(1024, 1);

        // 创建订阅者
        Subscriber subscriber1 = new DefaultSubscriber("subscriber-1",
                event -> System.out.println("Sub1收到消息: " + event.getMessage() + " [主题: " + event.getTopic() + "]"),
                error -> System.err.println("Sub1错误: " + error.getMessage())
        );

        Subscriber subscriber2 = new DefaultSubscriber("subscriber-2",
                event -> System.out.println("Sub2处理消息: " + event.getMessageId()),
                error -> System.err.println("Sub2错误: " + error.getMessage())
        );

        // 订阅主题
        system.subscribe("news", subscriber1);
        system.subscribe("news", subscriber2);
        // system.subscribe("sports", subscriber1);

        // 发布消息
//        for (int i = 0; i < 1; i++) {
            system.publish("news", "新闻消息 " + 1);
            system.publish("sss", "sss " + 1);
//        }

        // 查看系统状态
        SystemStatus status = system.getStatus();
        System.out.println("系统状态: " + status.isRunning() +
                ", 缓冲区: " + status.getRemainingCapacity() + "/" + status.getBufferSize() +
                ", 主题数: " + status.getTopicCount());

        // 等待消息处理完成
        Thread.sleep(1000);

        // 关闭系统
        system.shutdown();
    }

}
